package com.audition.thread;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author xukang
 * @date 2021-03-08 11:23
 */
public class BlockingQueueDemo {
    private ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<Object>(10);

    Runnable producer = () -> {

        try {
            while (true) {

                arrayBlockingQueue.put(new Object());
                System.out.println("生产");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
    Runnable consumer = () -> {
        while (true) {

            try {
                arrayBlockingQueue.take();
                System.out.println("消费");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

   static class Demo {
        public static void main(String[] args) {
            BlockingQueueDemo blockingQueueDemo = new BlockingQueueDemo();

            new Thread(blockingQueueDemo.producer).start();
            new Thread(blockingQueueDemo.consumer).start();
        }
    }
}
